<?php

class Admin_AdvertisementController extends Zend_Controller_Action
{

	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}
	
    public function init()
    {
        /* Initialize action controller here */
    	$this->_model = new Default_Model_DbTable_Advertisement();
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->USERNAME;
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Quảng Cáo","PREPEND");
        $this->view->title = "Quảng Cáo";
        $adv = $this->_model->GetAllAdv();
        $paginator = Zend_Paginator::factory($adv);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allAdv = $paginator;
    }
    
    public function addAction(){
    	$this->view->headTitle("Thêm Mới Quảng Cáo","PREPEND");
    	$this->view->title = "Thêm Mới Quảng Cáo";
    	$upload = new Private_Upload();
    	$mUser = new Default_Model_DbTable_User();
    	$this->view->userall = $mUser->GetAll();
    	if($this->getRequest()->isPost()){
    		$advData = $this->_request->getPost();
    		$UId = $this->_request->getParam("IDUser");
    		if($this->_getParam("NameAdv") == ""){
    			return $this->view->errorAdv =  "Bạn Chưa Nhập Thông Tin Quảng Cáo";
    		}else{
    			$advName = $this->_request->getParam("NameAdv");
    		}
    		if(!is_numeric($this->_getParam("Position"))){
    			return $this->view->errPos = "Bạn phải nhập vào 1 số";
    		}else{
    			$position = $this->_request->getParam("Position");
    		}
    		if($this->_getParam("CompName")== ""){
    			return $this->view->errComp = "Bạn chưa điền thông tin";
    		}else {
    			$compName = $this->_request->getParam("CompName");
    		}
    		$desc = $this->_request->getParam("desc");
    		if($desc == ""){
    			return $this->view->errDesc = "Bạn Chưa Nhập Mô Tả";
    		}
    		if($_FILES['AdvImg']['name']==null and $this->_getParam("Link")==""){
    			$img = "/ishali/public/uploaded/images/no_image.jpg";
    		}elseif(isset($_FILES["AdvImg"]["name"])){
    			$img = "/ishali/public/uploaded/images/advertisement/".$_FILES['AdvImg']["name"];
    			$upload->upload("advertisement");
    		}else {
    			$img = $this->_getParam("Link");
    		}
    		$stt = $this->_request->getParam("stt");
    		$this->_model->InsertAdv($UId, $advName, $img, $position, $compName, $desc, $stt);
    		$this->_redirect("/admin/advertisement");
    	}
    	
    }
    
    public function editAction(){
    	$this->view->headTitle("Chỉnh Sửa Quảng Cáo","PREPEND");
    	$this->view->title = "Chỉnh Sửa Quảng Cáo";
    	$upload = new Private_Upload();
    	$mUser = new Default_Model_DbTable_User();
    	$this->view->userall = $mUser->GetAll();
    	$aId = $this->_getParam("id",0);
    	$adv = $this->_model->GetById($aId);
    	$this->view->Adv = $adv;
    	 
    	if($this->getRequest()->isPost()){
    		$advData = $this->_request->getPost();
    		$UId = $this->_request->getParam("IDUser");
    		if($this->_getParam("NameAdv") == ""){
    			return $this->view->errorAdv =  "Bạn Chưa Nhập Thông Tin Quảng Cáo";
    		}else{
    			$advName = $this->_request->getParam("NameAdv");
    		}
    		if(!is_numeric($this->_getParam("Position"))){
    			return $this->view->errPos = "Bạn phải nhập vào 1 số";
    		}else{
    			$position = $this->_request->getParam("Position");
    		}
    		if($this->_getParam("CompName")== ""){
    			return $this->view->errComp = "Bạn chưa điền thông tin";
    		}else {
    			$compName = $this->_request->getParam("CompName");
    		}
    		$desc = $this->_request->getParam("desc");
    		if($desc == ""){
    			return $this->view->errDesc = "Bạn Chưa Nhập Mô Tả";
    		}
    		$stt = $this->_request->getParam("stt");
    		if($_FILES['AdvImg']['name']==null and $this->_getParam("Link")==""){
    			$img = "/ishali/public/uploaded/images/no_image.jpg";
    			$this->_model->UpdateAdv($aId, $UId, $advName, $img, $position, $compName, $desc, $stt);
    			$this->_redirect("/admin/advertisement");
    		}elseif(isset($_FILES["AdvImg"]["name"])){
    			$img = "/ishali/public/uploaded/images/advertisement/".$_FILES['AdvImg']["name"];
    			$upload->upload("advertisement");
    			$this->_model->UpdateAdv($aId, $UId, $advName, $img, $position, $compName, $desc, $stt);
    			$this->_redirect("/admin/advertisement");
    		}else {
    			$img = $this->_getParam("Link");
    			$this->_model->UpdateAdv($aId, $UId, $advName, $img, $position, $compName, $desc, $stt);
    			$this->_redirect("/admin/advertisement");
    		}
    		
    	}
    }
    

    public function delAction(){
    	$id = $this->_getParam("id",0);
    	$this->_model->DelAdv($id);
    	$this->_redirect("/admin/advertisement");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id",0);
    	$this->_model->UpdateStt($id);
    	$this->_redirect("/admin/advertisement");
    }
    

}

